package com.echo.tagparse;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.NoSuchElementException;

import edu.mit.jwi.Dictionary;
import edu.mit.jwi.IDictionary;
import edu.mit.jwi.item.IIndexWord;
import edu.mit.jwi.item.ISynset;
import edu.mit.jwi.item.IWord;
import edu.mit.jwi.item.IWordID;
import edu.mit.jwi.item.POS;

public class WordSynet{
	
	Hashtable<String, Integer> termSet = new Hashtable<String, Integer>();
	Hashtable<String, Integer> termSet1 = new Hashtable<String, Integer>();
	IDictionary dict = null;
	static String whome = "D:\\Word Net";
	
	public void openDict(String whome)throws MalformedURLException{
		String path = whome+ File.separator+ "dict";
		URL url = new URL("file", null, path);
		dict = new Dictionary(url);
		dict.open();
	}
	
	public void getSyn(String seed){
		if(termSet.get(seed)==null){
			termSet.put(seed, 0);
		}
		IIndexWord idxWord = dict.getIndexWord(seed, POS.ADJECTIVE);
		Iterator iter = idxWord.getWordIDs().iterator();
		while(iter.hasNext()){
			IWordID wordID = (IWordID)iter.next();
			IWord word = dict.getWord(wordID);
			ISynset synset = word.getSynset();
			
			for(IWord w: synset.getWords()){
//				System.out.println(w.getLemma());
				String tempWord = w.getLemma();
				if(termSet.get(tempWord)==null){
					termSet.put(tempWord, 1);
				}
			}
		}
	}
	
	public void secondRound(){
		for(Iterator iter = termSet.keySet().iterator(); iter.hasNext();){
			if(termSet.get((String)iter.next())==0){
				continue;
			}
			try{
				IIndexWord idxWord = dict.getIndexWord((String)iter.next(), POS.ADJECTIVE);
				Iterator it = idxWord.getWordIDs().iterator();
				while(it.hasNext()){
					IWordID wordID = (IWordID)it.next();
					IWord word = dict.getWord(wordID);
					ISynset synset = word.getSynset();
					
					for(IWord w: synset.getWords()){
						String tempWord = w.getLemma();
						if(termSet.get(tempWord)==null){
							termSet1.put(tempWord, 2);
						}
					}
				}
			}catch(NoSuchElementException e){
				continue;
			}
			
		}
		for(Iterator it = termSet1.keySet().iterator(); it.hasNext();){
			String key = (String)it.next();
			termSet.put(key, 2);
		}
		for(Iterator it = termSet.keySet().iterator(); it.hasNext();){
			String key = (String)it.next();
			System.out.println(key);
		}
		System.out.println(termSet.size());
	}
	
	public static void main(String[] args){
		try{
			WordSynet ws = new WordSynet();
			ws.openDict(whome);
			ws.getSyn("good");
			ws.secondRound();
		}catch(MalformedURLException e){
			e.printStackTrace();
		}
	}
	
}